package com.sky.aspect;

import com.sky.annotation.AutoFill;
import com.sky.constant.AutoFillConstant;
import com.sky.context.BaseContext;
import com.sky.enumeration.OperationType;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.time.LocalDateTime;

@Aspect
@Component
@Slf4j
public class AutoFillAspect {
    // 通用切入点表达式
    @Pointcut("execution(* com.sky.mapper.*.*(..)) && @annotation(com.sky.annotation.AutoFill)")
    public void autoFillPointCut() {
    }

    // 前置通知
    @Before("autoFillPointCut()")
    public void autoFill(JoinPoint joinPoint) {
        log.info("开始进行公共字段自动填充");
        // 获取到当前切面方法签名
        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
        // 获取到当前切面方法
        Method method = methodSignature.getMethod();
        // 获取到当前切面方法上的注解
        AutoFill autoFill = method.getAnnotation(AutoFill.class);
        // 获取到注解上定义的操作类型
        OperationType operationType = autoFill.value();
        //当前时间
        LocalDateTime localDateTime = LocalDateTime.now();
        //当前操作人
        Long currentId = BaseContext.getCurrentId();
        //获取当前方法参数
        Object entity = joinPoint.getArgs()[0];
        if (operationType == OperationType.INSERT) {
            //新增,需要自动填充的属性有:createTime,createUser,updateTime,updateUser
            try {
                //获取方法对应的set方法
                Method setCreateTime = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_CREATE_TIME, LocalDateTime.class);
                Method setUpdateTime = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_TIME, LocalDateTime.class);
                Method setCreateUser = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_CREATE_USER, Long.class);
                Method setUpdateUser = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_USER, Long.class);
                //调用set方法
                setCreateTime.invoke(entity, localDateTime);
                setUpdateTime.invoke(entity, localDateTime);
                setCreateUser.invoke(entity, currentId);
                setUpdateUser.invoke(entity, currentId);
            } catch (Exception e) {
                log.info("新增时自动填充出现错误:{}", e.getMessage());
            }
        }
        if (operationType == OperationType.UPDATE) {
            //修改,需要自动填充的属性有:updateTime,updateUser
            try {
                //获取方法对应的set方法
                Method setUpdateTime = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_TIME, LocalDateTime.class);
                Method setUpdateUser = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_USER, Long.class);
                //调用set方法
                setUpdateTime.invoke(entity, localDateTime);
                setUpdateUser.invoke(entity, currentId);
            } catch (Exception e) {
                log.info("修改时自动填充出现错误:{}", e.getMessage());
            }
        }
    }

}
